<?php

/**
 * 
 * Deposer une annonce
 * @author Redouane
 *
 */
class NewAdsForm extends AdsForm{
	public function configure(){
		$n_upload_dir = sfConfig::get('sf_root_dir').DIRECTORY_SEPARATOR.'www'.DIRECTORY_SEPARATOR.'uploads';
		$this->useFields(array('seller_id','title','town_id','type_id','category_id','subcategory_id','description','price','image_path1','image_path2', 'is_prof', 'bonplan', 'status_id'));
		
                $this->widgetSchema['category_id'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Category'), 'add_empty' => false), array('onChange'=>"selectSubCategories(this.value)"));
                $this->widgetSchema['subcategory_id']= new sfWidgetFormDoctrineChoice( 
                    array(  
                            'model' => $this->getRelatedModelName('SubCategory'),
                            'query' => Doctrine_Query::create()->from('SubCategory')->where('category_id=1'),
                        )
                );
                
                $this->widgetSchema['town_id'] = new sfWidgetFormDoctrineChoice(array( 
  			'model'     => 'Town',    
  			'order_by'  => array('num', 'asc'),    
  			'multiple'  => false,    
  			'add_empty' => false,
  			'method'  => 'getFullTitle',                                                                                                                                                
		)); 
		
		$this->widgetSchema['seller_id'] = new sfWidgetFormInputHidden();
		$this->widgetSchema['status_id'] = new sfWidgetFormInputHidden(); 
		$this->widgetSchema['is_prof'] = new sfWidgetFormChoice( array( 'choices' => array(0=>'Non', 1=>'Oui'), 'multiple' => false, 'expanded' => true, ));
		$this->widgetSchema['bonplan'] = new sfWidgetFormChoice( array( 'choices' => array(0=>'Non', 1=>'Oui'), 'multiple' => false, 'expanded' => true, ));
		
		/* photo 1 */
  		$this->widgetSchema['image_path1'] = new sfWidgetFormInputFileEditable(array(
	        'label'     	=> 'Photo 1',
	        'file_src'  	=> '/uploads/image_path1/' . '/' . $this->getObject()->image_path1,
	        'is_image'  	=> true,
	        'edit_mode' 	=> !$this->isNew(),
	        'delete_label'  => 'Supprimer la photo<br class="clear"/>',
	        'template'  	=> '<div>%input%<br />%delete% %delete_label%<br class="clear"/></div>',
	    ));
                
	    $this->validatorSchema['image_path1_delete'] = new sfValidatorPass();
            
            /* image_path1 validator */
	    $this->validatorSchema['image_path1'] = new sfValidatorFile(array(
	    	'required'   => false,
			'path'       => $n_upload_dir . '/image_path1',
	    	'mime_types' => array('image/jpeg','image/gif','image/png','image/jpg','image/bmp'),
            'validated_file_class' => 'bay3ValidatedResizedFile'
	    ));
	    
            
	    /* photo 2 */
	    $this->widgetSchema['image_path2'] = new sfWidgetFormInputFileEditable(array(
	        'label'     	=> 'Photo 2',
	        'file_src'  	=> '/uploads/image_path2/' . $this->getObject()->image_path2,
	        'is_image'  	=> true,
	        'edit_mode' 	=> !$this->isNew(),
	        'delete_label' 	=> 'Supprimer la photo<br class="clear"/>',
	        'template'  	=> '<div>%input%<br />%delete% %delete_label%<br class="clear"/></div>',
	    ));
            
	    $this->validatorSchema['image_path2_delete'] = new sfValidatorPass();
            
	    $this->validatorSchema['image_path2'] = new sfValidatorFile(array(
	    	'required'   => false,
	    	'path'       => $n_upload_dir . '/image_path2',
	    	'mime_types' => array('image/jpeg','image/gif','image/png','image/jpg','image/bmp'),
            'validated_file_class' => 'bay3ValidatedResizedFile'
	    ));
	    
	    
	    
	    /* Validators */
	    $this->validatorSchema['title']  		= new sfValidatorString(array('required' => true));
	    $this->validatorSchema['description']  	= new sfValidatorString(array('required' => true));
	    $this->validatorSchema['type_id']  		= new sfValidatorString(array('required' => true));
	    $this->validatorSchema['category_id']  	= new sfValidatorString(array('required' => true));
	    $this->validatorSchema['subcategory_id']= new sfValidatorString(array('required' => true));
	    $this->validatorSchema['is_prof']		= new sfValidatorInteger(array('required' => false));
    	$this->validatorSchema['price'] 		= new sfValidatorInteger(array('required' => true));
    	$this->validatorSchema['town_id'] 		= new sfValidatorInteger(array('required' => true));
    	
    	/* Schema */
    	$this->validatorSchema['title']->setMessage('required', 'Veuillez renseigner le titre de votre annonce');
    	$this->validatorSchema['description']->setMessage('required', 'Veuillez renseigner une description pour votre annonce');
		$this->validatorSchema['price']->setMessage('required', 'Veuillez renseigner le prix');
		$this->validatorSchema['price']->setMessage('invalid', 'Prix invalide');
	    
		$this->widgetSchema->setLabels(
			array(
					'title' 		 => 'Titre',
					'town_id' 		 => 'Ville *',
					'type_id' 		 => 'Type',
					'category_id' 	 => 'Catégorie *',
					'subcategory_id' => 'Sous-Catégorie *',
					'description' 	 => 'Description',
					'price' 		 => 'Prix DA',
					'image_path1' 	 => 'Photo 1',
					'image_path2' 	 => 'Photo 2',
					'is_prof' 		 => 'Professionnel ?',
					'bonplan' 		 => 'Bon plan ?',
		));
		
		
		
		$this->widgetSchema->setNameFormat('newAds[%s]');
		$this->widgetSchema->setFormFormatterName('div');
		$this->widgetSchema->getFormFormatter()->setTranslationCatalogue('messages');
	}
}